package dlg.code.state.context;

import dlg.code.state.context.service.IOrderStateService;
import dlg.code.state.context.service.IReminderService;
import dlg.code.state.context.service.impl.DeliveredOrderStateService;
import dlg.code.state.context.service.impl.PaidOrderStateService;
import dlg.code.state.context.service.impl.UnpaidOrderStateService;
import dlg.code.state.domain.OrderInfo;
import dlg.code.state.enums.OrderStateEnum;
import dlg.code.state.enums.ReminderStageEnum;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 状态模式
 * 改变对象内部的状态，让对象能产生不同的行为
 */
public class OrderStateContext {
    private final static ConcurrentHashMap<OrderStateEnum, IOrderStateService> orderStateMap = new ConcurrentHashMap<>();

    /**
     * springboot中可以自动使用注入
     */
    static {
        orderStateMap.put(OrderStateEnum.UNPAID, new UnpaidOrderStateService());
        orderStateMap.put(OrderStateEnum.PAID, new PaidOrderStateService());
        orderStateMap.put(OrderStateEnum.DELIVERED, new DeliveredOrderStateService());
    }

    public static IOrderStateService getOrderStateService(OrderInfo orderInfo) {
        OrderStateEnum orderStateEnum = orderInfo.getOrderStateEnum();
        IOrderStateService iOrderStateService = orderStateMap.get(orderStateEnum);
        if (iOrderStateService == null) {
            throw new IllegalArgumentException("订单状态异常：" + orderInfo.getOrderStateEnum());
        }
        return iOrderStateService;
    }
}
